package xyz.xuetu.www.consumer;

import xyz.xuetu.www.common.model.User;
import xyz.xuetu.www.common.service.UserService;
import xyz.xuetu.www.rpc.RpcApplication;
import xyz.xuetu.www.rpc.bootstrap.ConsumerBootstrap;
import xyz.xuetu.www.rpc.fault.retry.RetryStrategy;
import xyz.xuetu.www.rpc.fault.retry.RetryStrategyFactory;
import xyz.xuetu.www.rpc.model.RpcResponse;
import xyz.xuetu.www.rpc.proxy.ServiceProxyFactory;

public class ConsumerExample {

    public static void getProxy() {
        // 服务消费者初始化
        ConsumerBootstrap.init();


        UserService userService = ServiceProxyFactory.getProxy(UserService.class);
        User user = new User(1L, "admin", "123");
        User newUser = userService.getOne(user);
        if(newUser != null) {
            System.err.println(newUser.getUsername());
        }else {
            System.err.println("null");
        }
        long defaultId = userService.getDefaultId();
        System.err.println(defaultId);
    }

    public static void retryStrategyTest() {
        RetryStrategy retryStrategy = RetryStrategyFactory.getInstance(RpcApplication.getRpcConfig().getRetryStrategy());
        try {
            RpcResponse rpcResponse = retryStrategy.doRetry(() -> {
                System.out.println("测试重试");
                throw new RuntimeException("模拟重试失败");
            });
            System.out.println(rpcResponse);
        } catch (Exception e) {
            System.out.println("重试多次失败");
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        getProxy();
    }

}
